package cn.controller;

import cn.basic.BaseController;
import cn.dao.NoticeDAO;
import cn.factory.DAOFactory;
import cn.model.TNotice;
import cn.model.VNoticeUser;
import cn.model.VUserRole;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 公告信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/notice")
public class NoticeController extends BaseController {


    @RequestMapping(value="/addnotice")
    public void AddNotice(
            String title,
            String noticecontent,
            String note) throws IOException {


        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        NoticeDAO notdao = DAOFactory.getNoticeDAO();
        TNotice notice =new TNotice();
        notice.setTitle(title);
        notice.setNoticecontent(noticecontent);
        notice.setUserid(user.getUserid());
        notice.setCreationTime(Toolsutil.getdatetimes());
        notice.setNote(note);

        if (notdao.addNotice(notice) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现一个的删除
     * @param userid 用户名
     * @throws IOException
     */
    @RequestMapping(value="/deletenotice")
    public void deleteNotice(
            String noticeid,
            HttpServletRequest request,
            HttpServletResponse response,Model model) throws IOException{
        NoticeDAO notdao = DAOFactory.getNoticeDAO();
        if (noticeid !=null && !noticeid.equals("")){
            notdao.deleteNatice(Integer.parseInt(noticeid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }

    @RequestMapping(value="/modifynotice")
    public void modifyNotice(
            Integer noticeid,
            String title,
            String noticecontent,
            String note,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TNotice notice =new TNotice();
        notice.setNoticeid(noticeid);
        notice.setTitle(title);
        notice.setNoticecontent(noticecontent);
        notice.setCreationTime(Toolsutil.getdatetimes());
        notice.setUserid(user.getUserid());
        notice.setNote(note);
        NoticeDAO notdao = DAOFactory.getNoticeDAO();
        if (notdao.modifyNatice(notice)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }
    @RequestMapping(value="/getanoticelistByPage")//映射路径,拦截post请求
    public void getApplyList(
            String noticetitle,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException {

        Expression exp = new Expression();
        if (noticetitle != null && !noticetitle.equals("")) {
            exp.andEqu("title", noticetitle, String.class);
        }

        NoticeDAO notdao = DAOFactory.getNoticeDAO();

        String wherecondition = exp.toString();
        List<VNoticeUser> list = notdao.getNatice(wherecondition, page, limit);
        int size = notdao.getNaticeAmount(wherecondition);
        if (list != null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }
}
